/**
 * 2015年10月18日19:48:42
 */
/* App Module */

var app = angular.module('organization_app', [
    'ui.router',
    'organization_directives',
    'organization_controllers',
    'organization_filters',
    'organization_services'
])
    .run(
    ['$rootScope', '$state', '$stateParams', '$window', '$location', '$log',
        function ($rootScope, $state, $stateParams, $window, $location, $log) {
            $rootScope.$state = $state;
            $rootScope.$stateParams = $stateParams;

            var locationChangestart = $rootScope.$on('$locationChangeStart', locationChangeStart);
            var locationChangesuc = $rootScope.$on('$locationChangeSuccess', locationChangeSuccess);

            function locationChangeStart(e) {
                //跳转前提示
                //$log.log($stateParams.uuid);
//                    if($stateParams.uuid){
//                        if(!confirm('确认离开此页面?未保存的数据可能丢失')){
//                            e.preventDefault();
//                        };
//                        $log.log('id is had');
//                    } else {
//                        //$log.log(e);
//                    }
            }
            function locationChangeSuccess(e) {
//                function GetQueryString(name) {
//                    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)","i");
//                    var r = window.location.search.substr(1).match(reg);
//                    if (r!=null) return (r[2]);return null;
//                }
//                if(GetQueryString("window_id")!=null){
//                    parent.window.iFrameHeight(GetQueryString("window_id"),document.body.scrollHeight);
//                }

            }

        }
    ]
)
    .config(//同步请求拦截器，统一处理错误提示
    ['$httpProvider', '$stateProvider', '$urlRouterProvider',
        function ($httpProvider, $stateProvider, $urlRouterProvider) {
            $httpProvider.interceptors.push(['$q', '$rootScope', function ($q, $rootScope) {
                return {
                    'request': function (config) {
                        return config;
                    },
                    'requestError': function (rejection) {
                        var response = {
                            "data": {
                                "status": "error",
                                "description": "Request未知错误，请联系管理员",
                                "data": "null"
                            }
                        };
                        $rootScope.$broadcast('openGlobalErrorModal', response.data);
                        return $q.reject(rejection);
                        //return response;
                    },
                    'response': function (response) {

                        if (typeof response.data === 'string') {
                            if (response.data.indexOf instanceof Function &&
                                response.data.indexOf('<title>login</title>') !== -1) {
                                $rootScope.$broadcast('openGlobalLoginModal');
                                return $q.reject(response);
                            }
                        }
                        if (response.data.status === "ok") {
                            return response;
                        } else if (response.data.status === "info") {
                            $rootScope.$broadcast('openGlobalInfoModal', response.data);
                            return response;
                        } else if (response.data.status === "error") {
                            $rootScope.$broadcast('openGlobalErrorModal', response.data);
                            return $q.reject(response);
                        } else if (response.data.status === "warning") {
                            $rootScope.$broadcast('openGlobalWarningModal', response.data);
                            return $q.reject(response);
                        } else if (response.data.status === "notLogin") {
                            $rootScope.$broadcast('openGlobalLoginModal');
                            return $q.reject(response);
                        } else {
                            return response;
                        }
                    },
                    'responseError': function (rejection) {

                        var response = {
                            "data": {
                                "status": "error",
                                "description": "Response捕获到HTTP标准错误，网络或服务器故障",
                                "data": "null"
                            }
                        };
                        $rootScope.$broadcast('openGlobalErrorModal', response.data);
                        return $q.reject(rejection);
                    }
                };
            }]);
            $stateProvider.state("index", {
                url: "/",
                    views: {
                        'level0': {
                            templateUrl: "partials/partial.html",
                            controller: "CtrlIndex"
                        }
                    }
            })
            .state("organizationNode", {
                url: "/organizationNode",
                views: {
                    'level0': {
                        templateUrl: "partials/organizationNode/partial.html",
                        controller: "CtrlOrganizationNode"
                    }
                }
            })
                .state("organizationNodeItem", {
                    url: "/organizationNode/:organizationNodeCode",
                    views: {
                        'level0': {
                            templateUrl: "partials/organizationNode/item/partial.html",
                            controller: "CtrlOrganizationNodeItem"
                        }
                    }
                })

            .state("organizationNodeItem.basic", {
                    url: "/organizationNode/:organizationNodeCode/basic",
                    views: {
                        'level1': {
                            templateUrl: "partials/organizationNode/item/basic/partial.html",
                            controller: "CtrlOrganizationNodeItemBasic"
                        }
                    }
             })
                .state("organizationNodeItem.history", {
                    url: "/organizationNode/:organizationNodeCode/history",
                    views: {
                        'level1': {
                            templateUrl: "partials/organizationNode/item/history/partial.html",
                            controller: "CtrlOrganizationNodeItemHistory"
                        }
                    }
                })

                .state("organizationNodeItem.position", {
                    url: "/organizationNode/:organizationNodeCode/position",
                    views: {
                        'level1': {
                            templateUrl: "partials/organizationNode/item/position/partial.html",
                            controller: "CtrlOrganizationNodeItemPosition"
                        }
                    }
                })

                .state("organizationNodeItem.employee", {
                    url: "/organizationNode/:organizationNodeCode/employee",
                    views: {
                        'level1': {
                            templateUrl: "partials/organizationNode/item/employee/partial.html",
                            controller: "CtrlOrganizationNodeItemEmployee"
                        }
                    }
                })
                .state("position", {
                    url: "/position",
                    views: {
                        'level0': {
                            templateUrl: "partials/position/partial.html",
                            controller: "CtrlPosition"
                        }
                    }
                })
                .state("positionItem", {
                    url: "/positionItem/:positionCode",
                    views: {
                        'level0': {
                            templateUrl: "partials/position/item/partial.html",
                            controller: "CtrlPositionItem"
                        }
                    }
                })
                .state("positionItem.basic", {
                    url: "/positionItem/:positionCode/basic",
                    views: {
                        'level1': {
                            templateUrl: "partials/position/item/basic/partial.html",
                            controller: "CtrlPositionItemBasic"
                        }
                    }
                })
            .state("404", {
                    url: "/404",
                    templateUrl: "partials/common/404.html"
            })
            ;
            $urlRouterProvider.otherwise('/');

        }
    ]
);